using UnityEngine;
using System.Collections;

public class ObstacleManager : MonoBehaviour {
	
	public GameObject obstaclePrototype0;
	public GameObject obstaclePrototype1;
	public GameObject obstaclePrototype2;
	public GameObject obstaclePrototype3;
	public GameObject obstaclePrototype4;
	
	
	public GameObject powerupPrototype0;
	public GameObject powerupPrototype1;
	public GameObject powerupPrototype2;
	
	private ArrayList obstacles = new ArrayList();
	private float switchTimer = 0f;
	// Use this for initialization
	void Start ()
	{
		addObstacles();
	}
	private void addObstacles()
	{
		Transform sphereTransform = GameObject.Find("Sphere").transform;
		for(int i = 0; i < Balance.OBSTACLES_COUNT; i++)
		{
			Vector3 position = new Vector3(0, Balance.SPHERE_RADIUS - Balance.OBSTACLES_HEIGHT * 1.1f, 0);
				
			Obstacle newObstacle = ((GameObject) Instantiate(getPrototype(), position, Quaternion.identity)).GetComponent<Obstacle>();
			while (true)
			{
				float angle;
				Vector3 axis;
				Random.rotation.ToAngleAxis(out angle, out axis);
				newObstacle.transform.RotateAround(Vector3.zero, axis, angle);
				bool hasCloseNeighbors = false;
				foreach(Obstacle existingObstacle in obstacles)
					hasCloseNeighbors |= (newObstacle.transform.position - existingObstacle.transform.position).magnitude < Balance.OBSTACLES_MINIMUMDISTANCE;
				if (!hasCloseNeighbors)
					break;
			}
					
			//newObstacle.transform.LookAt(sphereTransform);
			//newObstacle.transform.Translate(new Vector3());
			if(Random.Range(0, 10) >= 5)
				newObstacle.switchState();
			obstacles.Add(newObstacle);
		}
		/*
		//Am Ende werden die Powerups hinzugefügt

		for(int i = 0; i < Balance.POWERUPS_COUNT; i++)
		{
			Vector3 position = new Vector3(0, Balance.SPHERE_RADIUS - Balance.OBSTACLES_HEIGHT * 1.1f, 0);
				
			Obstacle newObstacle = ((GameObject) Instantiate(getPowerup(), position, Quaternion.identity)).GetComponent<Obstacle>();
			while (true)
			{
				float angle;
				Vector3 axis;
				Random.rotation.ToAngleAxis(out angle, out axis);
				newObstacle.transform.RotateAround(Vector3.zero, axis, angle);
				bool hasCloseNeighbors = false;
				foreach(Obstacle existingObstacle in obstacles)
					hasCloseNeighbors |= (newObstacle.transform.position - existingObstacle.transform.position).magnitude < Balance.OBSTACLES_MINIMUMDISTANCE;
				if (!hasCloseNeighbors)
					break;
			}
					
			//newObstacle.transform.LookAt(sphereTransform);
			//newObstacle.transform.Translate(new Vector3());
			if(Random.Range(0, 10) >= 5)
				newObstacle.switchState();
			obstacles.Add(newObstacle);
		}	
	*/
	}
	// Update is called once per frame
	void Update ()
	{
		switchTimer += Time.deltaTime;
		if(switchTimer >= Balance.OBSTACLES_INTERVAL)
		{
			switchTimer = 0f;
			foreach(Obstacle existingObstacle in obstacles)
				existingObstacle.switchState();
		}	
	}
	public GameObject getPrototype()
	{
		switch(Random.Range(0, 4))
		{
			case 0:
				return obstaclePrototype0;
				break;
			case 1:
				return obstaclePrototype1;
				break;
			case 2:
				return obstaclePrototype2;
				break;
			case 3:
				return obstaclePrototype3;
				break;
			case 4:
				return obstaclePrototype4;
				break;
		default:
			throw new UnityException("Prototype index not implemented");
		}
	}

	public GameObject getPowerup()
	{
		switch(1)
		{
			case 0:
				return powerupPrototype0;
				break;
			case 1:
				return powerupPrototype1;
				break;
			case 2:
				return powerupPrototype2;
				break;
		default:
			throw new UnityException("Prototype index not implemented");
		}
	}

}
